<?php
class Dichvudao extends  Controller{
	
	public static function insert($item){
		$item->ngaythem = DateUtils::getNow();
		$item->trangthai = 1;
		if($item->validate()){
			return $item->save();
		}
		return false;
	}
	
	public static function delete($id){
		$dichvu = Dichvu::model()->findByPK($id);
		
		//xoa gia user
		$criteria = new CDbCriteria();
		$criteria->compare('iddichvu', $dichvu->id);
		$gias = GiadichvuUser::model()->findAll($criteria);
		foreach($gias as $item ){
			$item->delete();
		}
		
		//xoa mua dich vu
		$criteria = new CDbCriteria();
		$criteria->compare('iddichvu', $dichvu->id);
		$gias = Muadichvu::model()->findAll($criteria);
		foreach($gias as $item ){
			$item->delete();
		}
		
		$dichvu->delete();
	}
	
	public static function update($model){
		$dichvu = Dichvu::model()->findByPK($model->id);
		if($dichvu->gia != $model->gia){
			$criteria = new CDbCriteria();
			$criteria->compare('iddichvu', $dichvu->id);
			$gias = GiadichvuUser::model()->findAll($criteria);
			foreach($gias as $item ){
				$item->delete();
			}
		}
		$dichvu->gia =  $model->gia;
		$dichvu->tendichvu = $model->tendichvu;
		$dichvu->thoigian = $model->thoigian;
		$dichvu->loaicode = $model->loaicode;
		$dichvu->update();
	}
	
	public static function listHistory(){
		$iduser = UserUtils::getUserId();
		if($iduser === null){
			return '';
		}
		
		$criteria = new CDbCriteria();
		$criteria->compare('iduser', $iduser);
		$Page = new CPagination(Muadichvu::model()->count($criteria));
		$criteria->order = 'ngaymua DESC';
		$Page->pageSize = 25;
		$Page->applyLimit($criteria);
		$data['listhistory'] =  Muadichvu::model()->findAll($criteria);
		$data['pages'] = $Page;

		return $data;
	}
	
	public static function listOrder($trangthai){
		$criteria = new CDbCriteria();
		$criteria->order = 'ngaycapnhat DESC, ngaymua DESC';
		$criteria->compare('trangthai',$trangthai);
		$Page = new CPagination(Muadichvu::model()->count($criteria));
		$Page->pageSize =25;
		$Page->applyLimit($criteria);
		//$Page->pageVar= $trangthai;
		$data['orders'] =  Muadichvu::model()->findAll($criteria);
		$data['pages'] = $Page;

		return $data;
	}
	
	public static function changeState($state, $arr, $comment){
		//echo $state;
		//die();
		for($i=0; $i < count($arr); $i++)
		{
			if($arr[$i] != ''){
			    $model = Muadichvu::model()->findByPK($arr[$i]);
			    $model->trangthai = $state;
			    $model->ngaycapnhat = DateUtils::getNow();
			    $model->ketqua = $comment;
			    $model->update();
			    
			    if($state==3){
			    	$cre = Credit::model()->findByAttributes(array('iduser'=>$model->iduser));
			    	$cre->sodu = $cre->sodu + $model->gia;
			    	$cre->update();
			    	
			    	$chitiet = new Chitietnapcredit;
			    	$chitiet->idcredit = $cre->id;
			    	$chitiet->soluong = $model->gia;
			    	$chitiet->ngaynap = DateUtils::getNow();
			    	$chitiet->hinhthucnap = "Hoàn trả số credit Refund: " + $model->noidung;
			    	$chitiet->save();
			    }
			    
			    if($state > 1 && $state < 12){
			    	EmailUtils::sendemailServiceChangeState( $model , $model->tbuser);
			    }
			}
		}	
	}
	
	public static function stateString($service){
		switch($service->trangthai){
			case '0': return "Đang xử lý";
			case '1': return "Pendding";
			case '12': return "Pendding";
			case '2': return "Done";
			case '3': return "Refund";
		}
	}
	
	public static function traCode($model, $code){
		$model->ketqua = $code;
		$model->trangthai = 2;
		 $model->ngaycapnhat = DateUtils::getNow();
		$model->update();
		EmailUtils::sendemailServiceChangeState( $model , $model->tbuser);
	}
	
	public static function getlist($issort){
		$criteria = new CDbCriteria();
		$criteria->order = 'idnhomdichvu ASC';
		if($issort){
			return Dichvu::model()->findAll($criteria);
		}else{
			return Dichvu::model()->findAll();
		}
	}
	
	public static function historyUser($username){
		$user = User::model()->findByAttributes(array('username'=>$username));
		$criteria = new CDbCriteria();
		$criteria->compare('idcredit', $user->tbcredit->id);
		$Page = new CPagination(Chitietnapcredit::model()->count($criteria));
		$criteria->order = 'ngaynap DESC';
		$Page->pageSize = 25;
		$Page->applyLimit($criteria);
		$data['history'] =  Chitietnapcredit::model()->findAll($criteria);
		$data['pages'] = $Page;

		return $data;
	}
}